﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HerGoS.Viaticos.Engine;
using HerGoS.ControlsUI;

namespace HerGoS.WebUI.Viaticos.TipoTransporte
{
    public partial class TipoTransporte_ABM : PageBase
    {

        #region Propiedades

        public HerGoS.Viaticos.Entities.Transporte Transporte
        {
            get
            {
                if (ViewState["Transporte"] != null)
                    return (HerGoS.Viaticos.Entities.Transporte)ViewState["Transporte"];
                return null;
            }
            set
            {
                ViewState["Transporte"] = value;
            }

        }

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["id"] == null)
                {
                    Transporte = new HerGoS.Viaticos.Entities.Transporte();
                }
                else
                {
                    var Id = Convert.ToInt32(Request.QueryString["id"]);
                    Transporte = GestorTransporte.Obtener(Id);
                    this.txtCodigo.Text = Transporte.Codigo;
                    this.txtDescripcion.Text = Transporte.Descripcion;
                    this.chkPasaje.Checked = Transporte.CONPASAJE;
                    this.chkCalculaKm.Checked = Transporte.CALCULOKMS;

                }
                Initialize();
            }

        }

        void Initialize()
        {
            this.btnGuardar.Settings(new ButtonExtText());
            this.btnCerrar.Settings(new ButtonExtText());
            LoadAutoria(Transporte);
        }


        protected void btnGuardar_Click(object sender, EventArgs e)
        {
            Transporte.Codigo = this.txtCodigo.Text;
            Transporte.Descripcion = this.txtDescripcion.Text;
            Transporte.CONPASAJE = this.chkPasaje.Checked;
            Transporte.CALCULOKMS = this.chkCalculaKm.Checked;


            var tipoTransporteExist = GestorTransporte.ExisteTransporte(Transporte.Codigo);
            if (tipoTransporteExist)
            {
                var script = @"$(function(){{parent.TipoTransporteDuplicado('{0}');}});";
                script = string.Format(script, Transporte.Codigo);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "closedialog", script, true);
                return;
            }




            GestorTransporte.Guardar(Transporte);

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "closedialog", "parent.IntervaloCerrarPopup();", true);

        }


        protected void btnCerrar_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "closedialog", "parent.CerrarPopup();", true);
        }

    }
}